package leetcode_1601_1700;

public class LeeCode_1616 {
    public static void main(String[] args) {
//        System.out.println(checkPalindromeFormation("ulacfda", "jizalua"));
        //System.out.println(checkPalindromeFormation("abcdqwerty", "qwertydcba"));
//        System.out.println(checkPalindromeFormation("abcefe", "efecba"));
//        System.out.println(checkPalindromeFormation("cddbcdbdc", "cdbccbddc"));
//        System.out.println(checkPalindromeFormation("pvhmupgqeltozftlmfjjde", "yjgpzbezspnnpszebzmhvp"));
//        aejbaal flrmkswrydwdkdwdyrwskmrlf qizjezd
//        uvebspq ckawkhbrtlqwblfwzfptanhig laabjea
        System.out.println(checkPalindromeFormation("aejbaalflrmkswrydwdkdwdyrwskmrlfqizjezd",
                "uvebspqckawkhbrtlqwblfwzfptanhiglaabjea"));

    }

    private static boolean checkPalindromeFormation(String a, String b) {
        int len = a.length();
        if (len == 1)
            return true;
        int i = 0, j = len - 1;
        for (i = 0, j = len - 1; i <= j && a.charAt(i) == b.charAt(j); i++, j--) {
        }
        if (i >= j) {
            return true;
        } else {
            String s1 = b.substring(i, j + 1);
            String s2 = new StringBuilder(s1).reverse().toString();
            if (s1.equals(s2))
                return true;
            s1 = a.substring(i, j + 1);
            s2 = new StringBuilder(s1).reverse().toString();
            if (s1.equals(s2))
                return true;
        }
        for (i = 0, j = len - 1; i <= j && a.charAt(j) == b.charAt(i); i++, j--) {
        }
        if (i >= j) {
            return true;
        } else {
            String s1 = b.substring(i, j + 1);
            String s2 = new StringBuilder(s1).reverse().toString();
            if (s1.equals(s2))
                return true;
            s1 = a.substring(i, j + 1);
            s2 = new StringBuilder(s1).reverse().toString();
            return s1.equals(s2);
        }
    }
}
